package demo03_collection

import scala.collection.mutable.ArrayBuffer

object Demo01_array {

  /**
   * 创建不可变数组
   */
  def createImmutableArray() = {
    //数组的静态初始化，数组的类型是有泛型的
    val arr1: Array[Int] = Array(1, 2, 3, 4, 5)

    //数组的动态初始化
    val arr2: Array[Int] = new Array[Int](5)
    arr2(0) = 6
    arr2(1) = 7
    arr2(2) = 8
    arr2(3) = 9
    arr2(4) = 10

//    for(i <- arr1) println(i)
//    for(i <- arr2) println(i)

    //不管是静态初始化还是动态初始化，只要索引超出长度，都会报数组越界异常
//    println(arr1(5))
//    println(arr2(5))
  }


  /**
   * 创建可变数组
   */
  def createMutableArray() = {
    //静态初始化
    val arr1:ArrayBuffer[Int] = ArrayBuffer(1,2,3,4,5)
    arr1.append(100)
    arr1.append(200)
    arr1.append(300)
    println(arr1)

    //动态初始化
    val arr2:ArrayBuffer[Int] = new ArrayBuffer[Int](5)
    arr2.append(6,7,8,9,10)
    arr2.append(6,700,800)
    println(arr2)
  }

  def main(args: Array[String]): Unit = {
    createMutableArray()
  }

}
